<template>
  <div class="login-register-container">
    <div class="form-container">
      <h2>{{ isLogin ? '登录' : '注册' }}</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">{{ isLogin ? '登录' : '注册' }}</el-button>
          <el-button @click="toggleForm">{{ isLogin ? '去注册' : '去登录' }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

export default {
  name: 'LoginRegister',
  setup() {
    const store = useStore();
    const router = useRouter();
    const isLogin = ref(true);
    const form = ref({
      username: '',
      password: '',
      confirmPassword: ''
    });
    const rules = {
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            if (value !== form.value.password) {
              callback(new Error('两次输入的密码不一致'));
            } else {
              callback();
            }
          },
          trigger: 'blur'
        }
      ]
    };

    const formRef = ref(null);

    const submitForm = () => {
      formRef.value.validate(async (valid) => {
        if (valid) {
          try {
            if (isLogin.value) {
              // 登录
              const response = await store.dispatch('login', form.value);
              ElMessage.success('登录成功');
              router.push('/keywordMeaningList');
            } else {
              // 注册
              const response = await store.dispatch('register', form.value);
              ElMessage.success('注册成功，请登录');
              toggleForm();
            }
          } catch (error) {
            ElMessage.error(error.message); // 这里会显示后端返回的具体错误
          }
        } else {
          return false;
        }
      });
    };

    const toggleForm = () => {
      isLogin.value = !isLogin.value;
      form.value = {
        username: '',
        password: '',
        confirmPassword: ''
      };
    };

    return {
      isLogin,
      form,
      rules,
      formRef,
      submitForm,
      toggleForm,
      store
    };
  }
};
</script>

<style scoped>
.login-register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.form-container {
  width: 400px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>